@extends('admin.layout')

@section('title', '日志详情')

@section('content')
<div class="space-y-6">
    <!-- 页面标题 -->
    <div class="md:flex md:items-center md:justify-between">
        <div class="flex-1 min-w-0">
            <h2 class="text-2xl font-bold leading-7 text-gray-900 sm:text-3xl sm:truncate">
                日志详情
            </h2>
            <p class="mt-1 text-sm text-gray-500">
                查看日志的详细信息
            </p>
        </div>
        <div class="mt-4 flex space-x-3 md:mt-0 md:ml-4">
            <a href="{{ route('admin.logs.index') }}" 
               class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path>
                </svg>
                返回列表
            </a>
            <button onclick="deleteLog({{ $log->id }})" 
                    class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500">
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                </svg>
                删除日志
            </button>
        </div>
    </div>

    <!-- 基本信息 -->
    <div class="bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">基本信息</h3>
            <p class="mt-1 text-sm text-gray-600">日志的基本信息</p>
        </div>
        <div class="px-6 py-4">
            <dl class="grid grid-cols-1 gap-x-4 gap-y-6 sm:grid-cols-2">
                <div>
                    <dt class="text-sm font-medium text-gray-500">日志ID</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ $log->id }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">创建时间</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ $log->created_at->format('Y-m-d H:i:s') }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">日志名称</dt>
                    <dd class="mt-1 text-sm text-gray-900">
                        @if($log->log_name)
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
                                {{ $log->log_name }}
                            </span>
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">事件类型</dt>
                    <dd class="mt-1 text-sm text-gray-900">
                        @if($log->event)
                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                {{ $log->event }}
                            </span>
                        @else
                            <span class="text-gray-500">-</span>
                        @endif
                    </dd>
                </div>
                <div class="sm:col-span-2">
                    <dt class="text-sm font-medium text-gray-500">描述</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ $log->description }}</dd>
                </div>
            </dl>
        </div>
    </div>

    <!-- 操作者信息 -->
    <div class="bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">操作者信息</h3>
            <p class="mt-1 text-sm text-gray-600">执行此操作的用户信息</p>
        </div>
        <div class="px-6 py-4">
            @if($log->causer)
                <div class="flex items-center space-x-4">
                    <div class="h-12 w-12 rounded-full bg-indigo-500 flex items-center justify-center">
                        <span class="text-lg font-medium text-white">{{ $log->causer->nickname[0] ?? 'U' }}</span>
                    </div>
                    <div>
                        <h4 class="text-lg font-medium text-gray-900">{{ $log->causer->nickname ?? '未知用户' }}</h4>
                        <p class="text-sm text-gray-500">ID: {{ $log->causer->id }}</p>
                        @if($log->causer->phone)
                            <p class="text-sm text-gray-500">电话: {{ $log->causer->phone }}</p>
                        @endif
                        @if($log->causer->email)
                            <p class="text-sm text-gray-500">邮箱: {{ $log->causer->email }}</p>
                        @endif
                    </div>
                </div>
            @else
                <div class="text-center py-8">
                    <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
                    </svg>
                    <h3 class="mt-2 text-sm font-medium text-gray-900">系统操作</h3>
                    <p class="mt-1 text-sm text-gray-500">此操作由系统自动执行</p>
                </div>
            @endif
        </div>
    </div>

    <!-- 操作对象信息 -->
    @if($log->subject)
    <div class="bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">操作对象</h3>
            <p class="mt-1 text-sm text-gray-600">被操作的对象信息</p>
        </div>
        <div class="px-6 py-4">
            <dl class="grid grid-cols-1 gap-x-4 gap-y-6 sm:grid-cols-2">
                <div>
                    <dt class="text-sm font-medium text-gray-500">对象类型</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ class_basename($log->subject_type) }}</dd>
                </div>
                <div>
                    <dt class="text-sm font-medium text-gray-500">对象ID</dt>
                    <dd class="mt-1 text-sm text-gray-900">{{ $log->subject_id }}</dd>
                </div>
                @if($log->subject)
                    <div class="sm:col-span-2">
                        <dt class="text-sm font-medium text-gray-500">对象详情</dt>
                        <dd class="mt-1">
                            <div class="bg-gray-50 rounded-lg p-4">
                                <pre class="text-sm text-gray-900 whitespace-pre-wrap">{{ json_encode($log->subject->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
                            </div>
                        </dd>
                    </div>
                @endif
            </dl>
        </div>
    </div>
    @endif

    <!-- 属性信息 -->
    @if($log->properties && count($log->properties) > 0)
    <div class="bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">属性信息</h3>
            <p class="mt-1 text-sm text-gray-600">操作相关的属性数据</p>
        </div>
        <div class="px-6 py-4">
            <div class="bg-gray-50 rounded-lg p-4">
                <pre class="text-sm text-gray-900 whitespace-pre-wrap">{{ json_encode($log->properties, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
            </div>
        </div>
    </div>
    @endif

    <!-- 批次信息 -->
    @if($log->batch_uuid)
    <div class="bg-white shadow-lg rounded-xl border border-gray-100">
        <div class="px-6 py-6 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">批次信息</h3>
            <p class="mt-1 text-sm text-gray-600">批次操作相关信息</p>
        </div>
        <div class="px-6 py-4">
            <dl class="grid grid-cols-1 gap-x-4 gap-y-6 sm:grid-cols-2">
                <div>
                    <dt class="text-sm font-medium text-gray-500">批次UUID</dt>
                    <dd class="mt-1 text-sm text-gray-900 font-mono">{{ $log->batch_uuid }}</dd>
                </div>
            </dl>
        </div>
    </div>
    @endif
</div>

<script>
function deleteLog(id) {
    if (confirm('确定要删除这条日志吗？')) {
        fetch(`/admin/logs/${id}`, {
            method: 'DELETE',
            headers: {
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
                'Content-Type': 'application/json'
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                window.location.href = '/admin/logs';
            } else {
                alert('删除失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('删除失败');
        });
    }
}
</script>
@endsection
